/* File: PreviewPane.java
 * Date      Author        Changes
 * 12/09/10  Will Goodwin  Created
 */

package csc420groupproject;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * A pane for previewing multiple images.
 *
 * @version 1.0   December 9, 2010
 * @author William L. Goodwin
 */
public class PreviewPane extends JPanel implements ActionListener {
    private int current = 0; // the current image
    private ImageIcon[] images;
    private JButton next, prev; // navigation buttons
    private JLabel currentImage;
    private JPanel navPanel;

    /**
     * Creates a new PreviewPane with a list of image urls.
     *
     * @param list the list of images.
     */
    public PreviewPane(String[] list) {
	images = new ImageIcon[list.length];

	for (int i = 0; i < list.length; ++i)
	    images[i] = new ImageIcon(list[i]);
	currentImage = new JLabel(images[0]);

	next = new JButton("Next");
	next.addActionListener(this);

	prev = new JButton("Prev");
	prev.addActionListener(this);
	prev.setEnabled(false);
	
	this.setLayout(new BorderLayout());
	this.add(currentImage, BorderLayout.CENTER);

	navPanel = new JPanel(new GridLayout(1,2));
	navPanel.add(prev);
	navPanel.add(next);

	this.add(navPanel, BorderLayout.NORTH);
    }

    /* actions */
    public void actionPerformed(ActionEvent e) {
	if (((JButton)e.getSource()).equals(next))
	    ++current;
	else
	    --current;
	currentImage.setIcon(images[current]);
	
	if (current == images.length-1)
	    next.setEnabled(false);
	else if (current == 1)
	    prev.setEnabled(true);
	else if (current == 0)
	    prev.setEnabled(false);
	else
	    next.setEnabled(true);
    }
}

